package com.cn.quartz;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONException;
import org.quartz.JobDataMap;
import org.quartz.SchedulerException;
import org.springframework.transaction.annotation.Transactional;

import com.cn.jms.SendSMS;
import com.cn.struts2.SMSSendTimerAction;

public class SMSSendTimer {
	/**
	 * 定时器实例
	 */
	public SMSSendTimerAction smsSendTimerAction;
	/**
	 * 工作流实例
	 */
	public SMSSendJob job;
	/**
	 * 发送消息实例
	 */
	public SendSMS sendSMS;
	/**
	 * 取消状态
	 */
	private String cancelState = "2";
	/**
	 * 发送状态
	 */
	private String sendState = "1";

	/**
	 * 添加发送短信定时器
	 * 
	 * @param time
	 *            发送短信时间
	 * @param json
	 *            发送短信json
	 * @param userID
	 *            用户ID
	 * @return String:failure or success
	 * @throws JSONException 
	 */
	@Transactional(rollbackForClassName = "Exception")
	public String addSMSSendJob(String time, String json, String userID) throws JSONException {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		Date d = new Date();
		String jobName = dateFormat.format(d);

		if ("success".equals(smsSendTimerAction.addSMSTimer(json, time,
				jobName, userID))) {
			try {
				// SMSSendJob job = new SMSSendJob(jobName,json);
				JobDataMap jdMap = new JobDataMap();
				jdMap.put("jsonStr", json);
				jdMap.put("jobName", jobName);
				jdMap.put("sendSMS", sendSMS);
				jdMap.put("smsSendTimerAction", smsSendTimerAction);
				QuartzManage.addJobForSimpleTrigger(jobName, job, time, 1000,
						0, json, jdMap);
			} catch (SchedulerException e) {
				e.printStackTrace();
				return "failure";
			} catch (ParseException e) {
				e.printStackTrace();
				return "failure";
			}
			return "success";
		} else {
			return "failure";
		}
	}

	/**
	 * 修改定时发送短信任务
	 * 
	 * @param jobName
	 *            任务名称
	 * @param time
	 *            修改为的发送时间
	 * @return String:failure or success
	 */
	@Transactional(rollbackForClassName = "Exception")
	public String modifySMSSendJob(String jobName, String time) {
		if ("success".equals(smsSendTimerAction.modifySMSTimer(jobName, time))) {

			try {
				QuartzManage.modifyJobTimeForSimple(jobName, time);
				// /////////////////////////////////temp
				QuartzManage.modifyJobJsonForSimple(jobName, "jsonStr");
				// ////////////////////////////temp
			} catch (SchedulerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "failure";
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "failure";
			}
			return "success";
		} else {
			return "failure";
		}
	}

	/***
	 * 删除定时发送短信任务
	 * 
	 * @param jobName
	 *            任务名称
	 * @return String:failure or success
	 */
	@Transactional(rollbackForClassName = "Exception")
	public String deleSMSSendJob(String jobName) {
		if ("success".equals(smsSendTimerAction.deleSMSTimer(jobName))) {
			try {
				QuartzManage.removeJob(jobName);
			} catch (SchedulerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "failure";
			}
			return "success";
		} else {
			return "failure";
		}
	}

	/**
	 * 取消定时发送短信任务
	 * 
	 * @param jobName
	 *            任务名称
	 * @return String:failure or success
	 */
	@Transactional(rollbackForClassName = "Exception")
	public String cancelSMSSendJob(String jobName) {
		if ("success".equals(smsSendTimerAction.modifyState(jobName,
				cancelState))) {
			try {
				QuartzManage.removeJob(jobName);

			} catch (SchedulerException e) {
				e.printStackTrace();
				return "failure";
			}
			return "success";
		} else {
			return "failure";
		}
	}

	public SMSSendTimerAction getSmsSendTimerAction() {
		return smsSendTimerAction;
	}

	public void setSmsSendTimerAction(SMSSendTimerAction smsSendTimerAction) {
		this.smsSendTimerAction = smsSendTimerAction;
	}

	public SMSSendJob getJob() {
		return job;
	}

	public void setJob(SMSSendJob job) {
		this.job = job;
	}

	public SendSMS getSendSMS() {
		return sendSMS;
	}

	public void setSendSMS(SendSMS sendSMS) {
		this.sendSMS = sendSMS;
	}

	public static void main(String[] args) throws JSONException {
		SMSSendTimer sst = new SMSSendTimer();
		sst.addSMSSendJob("20140919103100", null, null);
	}
}
